package org.example.agent.task;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSON;
import org.example.constant.CommonConstant;
import org.example.dto.TaskStatus;
import org.example.enums.EDeployTaskStep;
import org.example.enums.EJobType;
import org.example.enums.ETaskErrCode;
import org.example.enums.ETaskStatus;
import org.example.model.FileConfigParam;
import org.example.model.UnzipFileParam;
import org.example.task.HttpTask;
import org.example.task.ITask;
import org.example.util.ThreadUtil;
import org.example.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileConfigTask extends ParentTask implements ITask {

    private static final Logger LOGGER = LoggerFactory.getLogger(FileConfigTask.class);

    private FileConfigParam fileConfigParam ;

    private Result<TaskStatus> taskResult;

    public FileConfigTask() {
    }

    public FileConfigTask(String params) {
        this.fileConfigParam = JSON.parseObject(HexUtil.decodeHexStr(params, CharsetUtil.CHARSET_UTF_8), FileConfigParam.class);
    }

    @Override
    public Result<TaskStatus> execute() {
        LOGGER.info("FileConfigTask 。。。start ,params={}", fileConfigParam.toString());
        Result<TaskStatus> result = Result.ok();
        ThreadUtil.threadWaitSeconds(10);
        this.taskResult = result;
        LOGGER.info("FileConfigTask 。。。end");
        return result;
    }

    @Override
    public Result<String> reportStatus() {
        this.taskResult = handleResult(this.taskResult, ETaskErrCode.FILECONFIG);
        TaskStatus taskStatus = taskResult.getResult();
        taskStatus.setJobId(fileConfigParam.getJobId());
        taskStatus.setTaskId(fileConfigParam.getTaskId());
        taskStatus.setJobType(EJobType.DEPLOY);
        taskStatus.setTaskStep(EDeployTaskStep.FILECONFIG.getStepNum());
        this.taskResult.setResult(taskStatus);
        HttpTask httpTask = new HttpTask(CommonConstant.MASTER_HOST_URL+"admincenter/plan/statereport", JSON.toJSONString(taskResult));
        Result<String> result = httpTask.execute();
        printErrMsg(result, EJobType.DEPLOY, EDeployTaskStep.FILECONFIG);
        LOGGER.info("FileConfigTask reportStatus taskResult={}",taskResult);
        return result;
    }


}
